package com.iwhalecloud.bss.kite.cucc.service.inst;

import com.iwhalecloud.bss.kite.cucc.client.api.inst.IMainOfferInstService;
import com.iwhalecloud.bss.kite.dataservice.entity.inst.OfferInst;
import com.iwhalecloud.bss.kite.dataservice.entity.inst.OrdOfferInst;
import com.iwhalecloud.bss.kite.dataservice.mapper.ord.OrdOfferInstMapper;
import com.iwhalecloud.bss.kite.manager.api.IQuerySceneInstService;
import com.iwhalecloud.bss.kite.manager.inst.KiteOfferInst;
import com.ztesoft.bss.common.util.SpringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.Optional;

@Service
public class MainOfferInstService implements IMainOfferInstService {

	@Autowired
    private IQuerySceneInstService querySceneInstService;

	@Override
	public KiteOfferInst getMainOfferInst(String sceneInstId) {
		String custId = querySceneInstService.getSceneInst(sceneInstId).getCustId();
		String mainOfferInstId = querySceneInstService.getExtParameterValue(sceneInstId, "mainOfferInstId");
		return querySceneInstService.getOfferInst(custId, mainOfferInstId);
	}

	@Override
	public KiteOfferInst getSendMainOfferInst(String sceneInstId) {
		String custId = querySceneInstService.getSceneInst(sceneInstId).getCustId();
		String mainOfferInstId = querySceneInstService.getExtParameterValue(sceneInstId, "sendOfferInstId");
		return querySceneInstService.getOfferInst(custId, mainOfferInstId);
	}

	@Override
	public KiteOfferInst getReceiveMainOfferInst(String sceneInstId) {
		String custId = querySceneInstService.getSceneInst(sceneInstId).getCustId();
		String mainOfferInstId = querySceneInstService.getExtParameterValue(sceneInstId, "receiveOfferInstId");
		return querySceneInstService.getOfferInst(custId, mainOfferInstId);
	}

	@Override
	public String getCustOrderIdByOfferInstId(String offerInstId, String custId) {
		Example example = new Example(OfferInst.class);
		example.createCriteria().andEqualTo("offerInstId", offerInstId).andEqualTo("ownerCustId", custId);
		OrdOfferInst offerInst = SpringUtil.getBean(OrdOfferInstMapper.class).selectOneByExample(example);
		return Optional.ofNullable(offerInst).map((inst) -> inst.getCustOrderId()).orElse(null);
	}


}
